/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmConceptos.java
 *
 * Created on 22/10/2012, 10:31:28 PM
 */

package co.edu.unitecnologica.interfaz;

import co.edu.unitecnologica.modelo.Conceptos;
import co.edu.unitecnologica.modelo.TiposConceptos;
import java.util.Calendar;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author osalcedo
 */
public class FrmConceptos extends Formulario {

    /** Creates new form FrmConceptos */
    public FrmConceptos(Principal p) {
        super(p);
        initComponents();
        habilitarCampos(false);
        init();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jmiTiposConceptos = new javax.swing.JMenuItem();
        grupoModoCalculo = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jtfCod = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jtfDescripcion = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jtfCodAuxiliar = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jtfCentroCosto = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jtfNIT = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jtfTipoCuenta = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jtfNroCuenta = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jrbValor = new javax.swing.JRadioButton();
        jrbPorcentaje = new javax.swing.JRadioButton();
        jLabel7 = new javax.swing.JLabel();
        jcbTipoConcepto = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        jckProcesarGerencia = new javax.swing.JCheckBox();
        jcAnulvariacion = new javax.swing.JCheckBox();

        jmiTiposConceptos.setText("Tipos de Conceptos");
        jmiTiposConceptos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiTiposConceptosActionPerformed(evt);
            }
        });

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Conceptos");

        jLabel1.setText("Codigo:");

        jLabel2.setText("Descripción:");

        jLabel3.setText("Cod. Auxiliar:");

        jLabel4.setText("Centro Costo:");

        jLabel5.setText("NIT:");

        jLabel6.setText("Tipo Cuenta:");

        jLabel8.setText("Nro Cuenta:");

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jrbValor.setText("Valor pleno");

        jrbPorcentaje.setText("Porcentaje de matricula");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jrbPorcentaje, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jrbValor, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(20, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jrbValor, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
                .addComponent(jrbPorcentaje, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jLabel7.setText("Modo de calculo");

        jLabel9.setText("Tipo de Concepto:");

        jckProcesarGerencia.setText("Procesa Gerencia electrónica");

        jcAnulvariacion.setText("Anula variacion de conceptos");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel8)
                    .addComponent(jLabel6)
                    .addComponent(jLabel5)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtfDescripcion, javax.swing.GroupLayout.DEFAULT_SIZE, 314, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jtfNroCuenta, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE)
                            .addComponent(jtfTipoCuenta, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE)
                            .addComponent(jtfCentroCosto, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE)
                            .addComponent(jtfCodAuxiliar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE)
                            .addComponent(jtfNIT, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel7)
                            .addComponent(jckProcesarGerencia, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE)
                            .addComponent(jcAnulvariacion)))
                    .addComponent(jtfCod, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jcbTipoConcepto, 0, 314, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtfCod, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jtfDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jcbTipoConcepto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel9))
                        .addGap(6, 6, 6)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jtfCodAuxiliar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jtfCentroCosto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(jtfNIT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(jtfTipoCuenta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jtfNroCuenta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jckProcesarGerencia))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jcAnulvariacion)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jmiTiposConceptosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmiTiposConceptosActionPerformed
        if(principal.validarObjeto("FrmTipoConceptos")){
            FrmTipoConceptos t= new FrmTipoConceptos(principal);
            principal.getEscritorio().add(t);
            t.setVisible(true);
        }
        else
            JOptionPane.showMessageDialog(rootPane, "Lo siento, pero su cuenta no tiene permisos para acceder a este Objeto");

    }//GEN-LAST:event_jmiTiposConceptosActionPerformed

    @Override
    public boolean insertarRegistro() {
        try{
            em.getTransaction().begin();
            Conceptos t= new Conceptos();
            t.setCod(Integer.parseInt(jtfCod.getText()));
            t.setDescripcion(jtfDescripcion.getText());
            if(this.jrbPorcentaje.isSelected())
                t.setModoCalculo('P');
            if(this.jrbValor.isSelected())
                t.setModoCalculo('V');
            if(jckProcesarGerencia.isSelected())
                t.setGerenciaElectronica('Y');
            else
                t.setGerenciaElectronica('N');
            if(this.jcAnulvariacion.isSelected())
                t.setAplicaVariacion('N');
            else
                t.setAplicaVariacion('Y');
            int cod=1;
            if(this.jcbTipoConcepto.getSelectedIndex()>=0)
                cod=Integer.parseInt(this.jcbTipoConcepto.getSelectedItem().toString().split("-")[0]);
            TiposConceptos r=em.find(TiposConceptos.class,cod);
            t.setCodTipoConcepto(r);
            t.setCentroCosto(this.jtfCentroCosto.getText());
            t.setCodAuxiliar(this.jtfCodAuxiliar.getText());
            t.setNit(this.jtfNIT.getText());
            t.setNumeroCuenta(this.jtfNroCuenta.getText());
            t.setTipoCuenta(this.jtfTipoCuenta.getText());
            t.setFechaActividad(Calendar.getInstance().getTime());
            t.setHoraActividad(Calendar.getInstance().getTime());
            t.setUsuario("usuario");
            em.persist(t);
            em.getTransaction().commit();
            return true;
        }catch(Exception e){
            em.getTransaction().rollback();
            return false;
        }
    }

    @Override
    public boolean buscarRegistros() {
        try{
            String sql="SELECT t.* "
                    + " FROM conceptos t"
                    + " WHERE "
                    + "     t.cod like '%"+jtfCod.getText()+"%' "
                    + "     and upper(t.descripcion) like upper('%"+jtfDescripcion.getText()+"%') ";
            query = em.createNativeQuery(sql,Conceptos.class);
            lista= query.getResultList();
            this.setRegistroActual(0);
            this.setRegistrosTotales(lista.size());
            this.mostrarRegistro(0);
            return true;
        }catch(Exception e){
            return false;
        }
    }

    @Override
    public boolean editarRegistro() {
        try{
            em.getTransaction().begin();
            Conceptos t= (Conceptos) lista.get(getRegistroActual());
            t.setCod(Integer.parseInt(jtfCod.getText()));
            t.setDescripcion(jtfDescripcion.getText());
            if(this.jrbPorcentaje.isSelected())
                t.setModoCalculo('P');
            if(this.jrbValor.isSelected())
                t.setModoCalculo('V');
            if(jckProcesarGerencia.isSelected())
                t.setGerenciaElectronica('Y');
            else
                t.setGerenciaElectronica('N');
            if(this.jcAnulvariacion.isSelected())
                t.setAplicaVariacion('N');
            else
                t.setAplicaVariacion('Y');
            int cod=1;
            if(this.jcbTipoConcepto.getSelectedIndex()>=0)
                cod=Integer.parseInt(this.jcbTipoConcepto.getSelectedItem().toString().split("-")[0]);
            TiposConceptos r=em.find(TiposConceptos.class,cod);
            t.setCodTipoConcepto(r);
            t.setCentroCosto(this.jtfCentroCosto.getText());
            t.setCodAuxiliar(this.jtfCodAuxiliar.getText());
            t.setNit(this.jtfNIT.getText());
            t.setNumeroCuenta(this.jtfNroCuenta.getText());
            t.setTipoCuenta(this.jtfTipoCuenta.getText());
            t.setFechaActividad(Calendar.getInstance().getTime());
            t.setHoraActividad(Calendar.getInstance().getTime());
            t.setUsuario("usuario");
            em.getTransaction().commit();
            return true;
        }catch(Exception e){
            em.getTransaction().rollback();
            return false;
        }
    }

    @Override
    public boolean borrarRegistro() {
        try{
            em.getTransaction().begin();
            Conceptos t= (Conceptos) lista.get(getRegistroActual());
            em.remove(t);
            em.getTransaction().commit();
            return true;
        }catch(Exception e){
            em.getTransaction().rollback();
            return false;
        }
    }

    @Override
    public void cargarRegistros() {
        query=em.createNamedQuery("Conceptos.findAll");
        lista=query.getResultList();
        setRegistroActual(0);
        setRegistrosTotales(lista.size());
        mostrarRegistro(getRegistroActual());
    }

    @Override
    public void limpiarCampos() {
        this.jtfCentroCosto.setText("");
        this.jtfCod.setText("");
        this.jtfCodAuxiliar.setText("");
        this.jtfDescripcion.setText("");
        this.jtfNIT.setText("");
        this.jtfTipoCuenta.setText("");
        this.jtfNroCuenta.setText("");
    }

    @Override
    public void habilitarCampos(boolean v) {
        this.jtfCentroCosto.setEditable(v);
        this.jtfCod.setEditable(v);
        this.jtfCodAuxiliar.setEditable(v);
        this.jtfDescripcion.setEditable(v);
        this.jtfNIT.setEditable(v);
        this.jtfNroCuenta.setEditable(v);
        this.jtfTipoCuenta.setEditable(v);
        this.jcbTipoConcepto.setEditable(v);
        this.jrbPorcentaje.setEnabled(v);
        this.jrbValor.setEnabled(v);
        this.jckProcesarGerencia.setEnabled(v);
        this.jcAnulvariacion.setEnabled(v);
    }

    @Override
    public void mostrarRegistro(int r) {
        if(lista.size()>0 && r<lista.size()){
            Conceptos t=(Conceptos) lista.get(r);
            jtfCod.setText(t.getCod().toString());
            jtfDescripcion.setText(t.getDescripcion());
            this.jtfCentroCosto.setText(t.getCentroCosto());
            this.jtfCodAuxiliar.setText(t.getCodAuxiliar());
            this.jtfNIT.setText(t.getNit());
            this.jtfNroCuenta.setText(t.getNumeroCuenta());
            this.jtfTipoCuenta.setText(t.getTipoCuenta());
            if(t.getModoCalculo()=='P')
                this.jrbPorcentaje.setSelected(true);
            else
                this.jrbValor.setSelected(true);
            if(t.getGerenciaElectronica()=='Y')
                jckProcesarGerencia.setSelected(true);
            else
                jckProcesarGerencia.setSelected(false);
            if(t.getAplicaVariacion()=='N')
                jcAnulvariacion.setSelected(true);
            else
                jcAnulvariacion.setSelected(false);
            jcbTipoConcepto.setSelectedItem(t.getCodTipoConcepto().getCod()+"-"+t.getCodTipoConcepto().getDescripcion());
        }
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup grupoModoCalculo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JCheckBox jcAnulvariacion;
    private javax.swing.JComboBox jcbTipoConcepto;
    private javax.swing.JCheckBox jckProcesarGerencia;
    private javax.swing.JMenuItem jmiTiposConceptos;
    private javax.swing.JRadioButton jrbPorcentaje;
    private javax.swing.JRadioButton jrbValor;
    private javax.swing.JTextField jtfCentroCosto;
    private javax.swing.JTextField jtfCod;
    private javax.swing.JTextField jtfCodAuxiliar;
    private javax.swing.JTextField jtfDescripcion;
    private javax.swing.JTextField jtfNIT;
    private javax.swing.JTextField jtfNroCuenta;
    private javax.swing.JTextField jtfTipoCuenta;
    // End of variables declaration//GEN-END:variables

    @Override
    public void agregarItemsMenuOpciones() {
        menuOpciones.add(jmiTiposConceptos);
    }

    private void init() {
        grupoModoCalculo.add(jrbValor);
        grupoModoCalculo.add(jrbPorcentaje);
        javax.persistence.Query q=em.createNamedQuery("TiposConceptos.findAll");
        List l=q.getResultList();
        for(int i=0;i<l.size();i++){
            TiposConceptos t= (TiposConceptos)l.get(i);
            jcbTipoConcepto.addItem(t.getCod()+"-"+t.getDescripcion());
        }
    }

}
